<?php
defined('IN_MAIN') or exit('Invalid Access!');global $arrTree;
global $arrTree2;
function generate_code($country){	$code = rand(1000000,9999999);	
	$exist = true;	while($exist==true){				$dist_code = $country.$code;				$rs = mysql_query("select id from gg_users where code='".$dist_code."'");				if($rs){			if(mysql_num_rows($rs)>0){								$code = rand(1000000,9999999);								$exist = true;				}else{								$exist = false;							}					}else{						$exist = false;					}			}	
	return $dist_code;
}

function create_matrix($new_uid){
	global $vars;
 	$db=$vars["db"];
	
	$sql="INSERT INTO $db->matrix_first (uid) VALUES (".$new_uid.")";
	if(!mysql_query($sql))
		$errmsg.=__("We have encountered some error and the registration process has been failed.")."<br />\nError: ".mysql_error()."<br />\n";
					
	$sql="INSERT INTO $db->matrix_second (uid) VALUES (".$new_uid.")";
	if(!mysql_query($sql))
		$errmsg.=__("We have encountered some error and the registration process has been failed.")."<br />\nError: ".mysql_error()."<br />\n";
					
	$sql="INSERT INTO $db->matrix_third (uid) VALUES (".$new_uid.")";
	if(!mysql_query($sql))
		$errmsg.=__("We have encountered some error and the registration process has been failed.")."<br />\nError: ".mysql_error()."<br />\n";
					
	$sql="INSERT INTO $db->matrix_fourth (uid) VALUES (".$new_uid.")";
	if(!mysql_query($sql))
		$errmsg.=__("We have encountered some error and the registration process has been failed.")."<br />\nError: ".mysql_error()."<br />\n";
					
	$sql="INSERT INTO $db->matrix_fifth (uid) VALUES (".$new_uid.")";
	if(!mysql_query($sql))
		$errmsg.=__("We have encountered some error and the registration process has been failed.")."<br />\nError: ".mysql_error()."<br />\n";
					
	$sql="INSERT INTO $db->matrix_sixth (uid) VALUES (".$new_uid.")";
	if(!mysql_query($sql))
		$errmsg.=__("We have encountered some error and the registration process has been failed.")."<br />\nError: ".mysql_error()."<br />\n";

	$sql="update gg_settings set value='".$sql."' where param='maintenance'";
	if(!mysql_query($sql))
		$errmsg.=__("We have encountered some error and the registration process has been failed.")."<br />\nError: ".mysql_error()."<br />\n";
}


function auto_matrix1($sponsor_id){
	global $vars;
 	$db=$vars["db"];
 	
	$sql = "SELECT id, ref, total_sponsor_1 FROM $db->users WHERE id=".$sponsor_id;
	$user_account=@mysql_fetch_assoc(mysql_query($sql));
	if($user_account['total_sponsor_1']=='2'){
		$sql = "SELECT id, lvl_1 FROM $db->matrix_first WHERE uid=".$user_account['id'];
		$user_matrix=@mysql_fetch_assoc(mysql_query($sql));
		if($user_matrix['lvl_1']=='0'){
			//own point
			$sql = "UPDATE $db->matrix_first SET lvl_1=".$user_account['id'].",date_1='".date('Y-m-d')."' WHERE id=".$user_matrix['id'];
			mysql_query($sql);
							
			//first point
			$sql = "SELECT * FROM $db->matrix_first WHERE uid=".$user_account['ref'];
			$sponsor_matrix=@mysql_fetch_assoc(mysql_query($sql));
			if($sponsor_matrix['lvl1_1']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl1_1=".$user_account['id'].",date1_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl1_2']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl1_2=".$user_account['id'].",date1_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl1_3']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl1_3=".$user_account['id'].",date1_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_1']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_1=".$user_account['id'].",date2_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_2']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_2=".$user_account['id'].",date2_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_3']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_3=".$user_account['id'].",date2_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_4']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_4=".$user_account['id'].",date2_4='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_5']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_5=".$user_account['id'].",date2_5='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_6']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_6=".$user_account['id'].",date2_6='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_7']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_7=".$user_account['id'].",date2_7='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_8']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_8=".$user_account['id'].",date2_8='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_9']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_9=".$user_account['id'].",date2_9='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}
			mysql_query($sql);
		}
						
		//second point
		$sql = "SELECT * FROM $db->matrix_first WHERE lvl1_1=".$user_account['ref'];
		$q = mysql_query($sql);							
		if(mysql_num_rows($q)>0){
			$sponsor_matrix=@mysql_fetch_assoc($q);
			if($sponsor_matrix['lvl2_1']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_1=".$user_account['id'].",date2_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_2']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_2=".$user_account['id'].",date2_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_3']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_3=".$user_account['id'].",date2_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}
		}
		//second point
		$sql = "SELECT * FROM $db->matrix_first WHERE lvl1_2=".$user_account['ref'];
		$q = mysql_query($sql);							
		if(mysql_num_rows($q)>0){
			$sponsor_matrix=@mysql_fetch_assoc($q);
			if($sponsor_matrix['lvl2_4']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_4=".$user_account['id'].",date2_4='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_5']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_5=".$user_account['id'].",date2_5='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_6']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_6=".$user_account['id'].",date2_6='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}
		}
		//second point
		$sql = "SELECT * FROM $db->matrix_first WHERE lvl1_3=".$user_account['ref'];
		$q = mysql_query($sql);							
		if(mysql_num_rows($q)>0){
			$sponsor_matrix=@mysql_fetch_assoc($q);
			if($sponsor_matrix['lvl2_7']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_7=".$user_account['id'].",date2_7='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_8']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_8=".$user_account['id'].",date2_8='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_9']=='0'){
				$sql = "UPDATE $db->matrix_first SET lvl2_9=".$user_account['id'].",date2_9='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}
		}
		mysql_query($sql);
	}
}

function auto_matrix4($sponsor_id){
	global $vars;
 	$db=$vars["db"];
 	
 	$sql = "SELECT id, ref, total_sponsor_2 FROM $db->users WHERE id=".$sponsor_id;
	$user_account=@mysql_fetch_assoc(mysql_query($sql));
	if($user_account['total_sponsor_2']=='2'){
		$sql = "SELECT id, lvl_1 FROM $db->matrix_fourth WHERE uid=".$user_account['id'];
		$user_matrix=@mysql_fetch_assoc(mysql_query($sql));
		if($user_matrix['lvl_1']=='0'){
			//own point
			$sql = "UPDATE $db->matrix_fourth SET lvl_1=".$user_account['id'].",date_1='".date('Y-m-d')."' WHERE id=".$user_matrix['id'];
			mysql_query($sql);
			
			//first point
			$sql = "SELECT * FROM $db->matrix_fourth WHERE uid=".$user_account['ref'];
			$sponsor_matrix=@mysql_fetch_assoc(mysql_query($sql));
			if($sponsor_matrix['lvl1_1']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl1_1=".$user_account['id'].",date1_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl1_2']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl1_2=".$user_account['id'].",date1_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl1_3']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl1_3=".$user_account['id'].",date1_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_1']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_1=".$user_account['id'].",date2_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_2']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_2=".$user_account['id'].",date2_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_3']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_3=".$user_account['id'].",date2_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_4']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_4=".$user_account['id'].",date2_4='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_5']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_5=".$user_account['id'].",date2_5='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_6']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_6=".$user_account['id'].",date2_6='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_7']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_7=".$user_account['id'].",date2_7='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_8']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_8=".$user_account['id'].",date2_8='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_9']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_9=".$user_account['id'].",date2_9='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}
			mysql_query($sql);
		}
		
		//second point
		$sql = "SELECT * FROM $db->matrix_fourth WHERE lvl1_1=".$user_account['ref'];
		$q = mysql_query($sql);							
		if(mysql_num_rows($q)>0){
			$sponsor_matrix=@mysql_fetch_assoc($q);
			if($sponsor_matrix['lvl2_1']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_1=".$user_account['id'].",date2_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_2']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_2=".$user_account['id'].",date2_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_3']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_3=".$user_account['id'].",date2_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}
			mysql_query($sql);
		}
		//second point
		$sql = "SELECT * FROM $db->matrix_fourth WHERE lvl1_2=".$user_account['ref'];
		$q = mysql_query($sql);							
		if(mysql_num_rows($q)>0){
			$sponsor_matrix=@mysql_fetch_assoc($q);
			if($sponsor_matrix['lvl2_4']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_4=".$user_account['id'].",date2_4='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_5']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_5=".$user_account['id'].",date2_5='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_6']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_6=".$user_account['id'].",date2_6='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}
			mysql_query($sql);
		}
		//second point
		$sql = "SELECT * FROM $db->matrix_fourth WHERE lvl1_3=".$user_account['ref'];
		$q = mysql_query($sql);							
		if(mysql_num_rows($q)>0){
			$sponsor_matrix=@mysql_fetch_assoc($q);
			if($sponsor_matrix['lvl2_7']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_7=".$user_account['id'].",date2_7='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_8']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_8=".$user_account['id'].",date2_8='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl2_9']=='0'){
				$sql = "UPDATE $db->matrix_fourth SET lvl2_9=".$user_account['id'].",date2_9='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}
			mysql_query($sql);
		}
				
		//check full matrix
		//$sql = "SELECT uid FROM $db->matrix_fourth WHERE lvl_1>0 AND lvl1_1>0 AND lvl1_2>0 AND lvl2_1>0 AND lvl2_2>0 AND lvl2_4>0 AND lvl2_5>0 AND is_qualify=0";
		$sql = "SELECT uid FROM $db->matrix_fourth WHERE lvl_1>0 AND lvl1_1>0 AND lvl1_2>0 AND ((lvl2_1>0 AND lvl2_2>0 AND lvl2_4>0 AND lvl2_5>0) OR (lvl2_4>0 AND lvl2_5>0 AND lvl2_7>0 AND lvl2_8>0) OR (lvl2_1>0 AND lvl2_2>0 AND lvl2_7>0 AND lvl2_8>0)) AND is_qualify=0";
		$q = mysql_query($sql);							
		if(mysql_num_rows($q)>0){
			while ($complete_matrix = mysql_fetch_assoc($q)) {
				auto_matrix5($complete_matrix["uid"]);				
			}
		}
	}
}

function auto_matrix5($user_id=0){	
	global $vars;
 	$db=$vars["db"];
 	
	if($user_id>0){
		$atree=array();
		array_push($atree,'1');
		
		while(sizeof($atree)) {
	
			$node=array_shift($atree);
		 	$nd=0;
			$np=1;
	
			$strsq1="select id,fifth_position from $db->users where fifth_upline=".$node." order by fifth_position";
			$rs = mysql_query($strsq1);	
	
			while ($member = mysql_fetch_assoc($rs)) {	
				array_push($atree,$member["id"]);
				$nd++;
				
				if($np==$member["fifth_position"] or $member["fifth_position"]==0) $np++;
			}

			if($np<=3 && $node<>'0') break;		
		}

		if($np<=3 && $node<>'0') {
			$sql = "UPDATE $db->users SET fifth_upline=".$node.",fifth_position=".$np.",fifth_date='".date('Y-m-d')."' WHERE id=".$user_id;
			mysql_query($sql);
			
			//own point
			$sql = "UPDATE $db->matrix_fifth SET lvl_1=".$user_id.",date_1='".date('Y-m-d')."' WHERE uid=".$user_id;
			mysql_query($sql);
			$sql = "UPDATE $db->matrix_fourth SET is_qualify=1 WHERE uid=".$user_id;				
			mysql_query($sql);
			
			//first point
			$sql = "SELECT * FROM $db->matrix_fifth WHERE uid=".$node;
			$sponsor_matrix=@mysql_fetch_assoc(mysql_query($sql));
			if($sponsor_matrix['lvl1_1']=='0' && $np=='1'){
				$sql = "UPDATE $db->matrix_fifth SET lvl1_1=".$user_id.",date1_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl1_2']=='0' && $np=='2'){
				$sql = "UPDATE $db->matrix_fifth SET lvl1_2=".$user_id.",date1_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl1_3']=='0' && $np=='3'){
				$sql = "UPDATE $db->matrix_fifth SET lvl1_3=".$user_id.",date1_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}
			mysql_query($sql);
			
			//second point
			$sql = "SELECT * FROM $db->matrix_fifth WHERE lvl1_1=".$node;
			$q = mysql_query($sql);							
			if(mysql_num_rows($q)>0){
				$sponsor_matrix=@mysql_fetch_assoc($q);
				if($sponsor_matrix['lvl2_1']=='0' && $np=='1'){
					$sql = "UPDATE $db->matrix_fifth SET lvl2_1=".$user_id.",date2_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_2']=='0' && $np=='2'){
					$sql = "UPDATE $db->matrix_fifth SET lvl2_2=".$user_id.",date2_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_3']=='0' && $np=='3'){
					$sql = "UPDATE $db->matrix_fifth SET lvl2_3=".$user_id.",date2_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}
				mysql_query($sql);
			}
			//second point
			$sql = "SELECT * FROM $db->matrix_fifth WHERE lvl1_2=".$node;
			$q = mysql_query($sql);							
			if(mysql_num_rows($q)>0){
				$sponsor_matrix=@mysql_fetch_assoc($q);
				if($sponsor_matrix['lvl2_4']=='0' && $np=='1'){
					$sql = "UPDATE $db->matrix_fifth SET lvl2_4=".$user_id.",date2_4='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_5']=='0' && $np=='2'){
					$sql = "UPDATE $db->matrix_fifth SET lvl2_5=".$user_id.",date2_5='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_6']=='0' && $np=='3'){
					$sql = "UPDATE $db->matrix_fifth SET lvl2_6=".$user_id.",date2_6='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}
				mysql_query($sql);
			}
			//second point
			$sql = "SELECT * FROM $db->matrix_fifth WHERE lvl1_3=".$node;
			$q = mysql_query($sql);
			if(mysql_num_rows($q)>0){
				$sponsor_matrix=@mysql_fetch_assoc($q);
				if($sponsor_matrix['lvl2_7']=='0' && $np=='1'){
					$sql = "UPDATE $db->matrix_fifth SET lvl2_7=".$user_id.",date2_7='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_8']=='0' && $np=='2'){
					$sql = "UPDATE $db->matrix_fifth SET lvl2_8=".$user_id.",date2_8='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_9']=='0' && $np=='3'){
					$sql = "UPDATE $db->matrix_fifth SET lvl2_9=".$user_id.",date2_9='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}
				mysql_query($sql);
			}
		}

		//check full matrix
		$sql = "SELECT uid FROM $db->matrix_fifth WHERE lvl_1>0 AND lvl1_1>0 AND lvl1_2>0 AND lvl1_3>0 AND lvl2_1>0 AND lvl2_2>0 AND lvl2_3>0 AND lvl2_4>0 AND lvl2_5>0 AND lvl2_6>0 AND lvl2_7>0 AND lvl2_8>0 AND lvl2_9>0 AND is_qualify=0";
		$q = mysql_query($sql);
		$sql2 = "SELECT uid FROM $db->matrix_fourth WHERE lvl_1>0 AND lvl1_1>0 AND lvl1_2>0 AND lvl1_3>0 AND lvl2_1>0 AND lvl2_2>0 AND lvl2_3>0 AND lvl2_4>0 AND lvl2_5>0 AND lvl2_6>0 AND lvl2_7>0 AND lvl2_8>0 AND lvl2_9>0 AND is_qualify=1";
		$q2 = mysql_query($sql2);							
		if(mysql_num_rows($q)>0 && mysql_num_rows($q2)>0){
			while ($complete_matrix = mysql_fetch_assoc($q)) {
				auto_matrix6($complete_matrix["uid"]);
				mysql_query("UPDATE $db->matrix_fifth SET is_qualify=1 WHERE uid=".$complete_matrix["uid"]);
			}
		}
	}
}

function auto_matrix6($user_id=0){	
	global $vars;
 	$db=$vars["db"];
 	
	if($user_id>0){
		$atree=array();
		array_push($atree,'44');
		
		while(sizeof($atree)) {
	
			$node=array_shift($atree);
		 	$nd=0;
			$np=1;
	
			$strsq1="select id,sixth_position from $db->users where sixth_upline=".$node." order by sixth_position";
			$rs = mysql_query($strsq1);	
	
			while ($member = mysql_fetch_assoc($rs)) {	
				array_push($atree,$member["id"]);
				$nd++;
				
				if($np==$member["sixth_position"] or $member["sixth_position"]==0) $np++;
			}

			if($np<=3 && $node<>'0') break;		
		}

		if($np<=3 && $node<>'0') {
			$sql = "UPDATE $db->users SET sixth_upline=".$node.",sixth_position=".$np.",sixth_date='".date('Y-m-d')."' WHERE id=".$user_id;
			mysql_query($sql);
			
			//own point
			$sql = "UPDATE $db->matrix_sixth SET lvl_1=".$user_id.",date_1='".date('Y-m-d')."' WHERE uid=".$user_id;
			mysql_query($sql);
			$sql = "UPDATE $db->matrix_fifth SET is_qualify=1 WHERE uid=".$user_id;				
			mysql_query($sql);
			/*
			//first point
			$sql = "SELECT * FROM $db->matrix_sixth WHERE uid=".$node;
			$sponsor_matrix=@mysql_fetch_assoc(mysql_query($sql));
			if($sponsor_matrix['lvl1_1']=='0' && $np=='1'){
				$sql = "UPDATE $db->matrix_sixth SET lvl1_1=".$user_id.",date1_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl1_2']=='0' && $np=='2'){
				$sql = "UPDATE $db->matrix_sixth SET lvl1_2=".$user_id.",date1_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}elseif($sponsor_matrix['lvl1_3']=='0' && $np=='3'){
				$sql = "UPDATE $db->matrix_sixth SET lvl1_3=".$user_id.",date1_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
			}
			mysql_query($sql);
			
			//second point
			$sql = "SELECT * FROM $db->matrix_sixth WHERE lvl1_1=".$node;
			$q = mysql_query($sql);							
			if(mysql_num_rows($q)>0){
				$sponsor_matrix=@mysql_fetch_assoc($q);
				if($sponsor_matrix['lvl2_1']=='0' && $np=='1'){
					$sql = "UPDATE $db->matrix_sixth SET lvl2_1=".$user_id.",date2_1='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_2']=='0' && $np=='2'){
					$sql = "UPDATE $db->matrix_sixth SET lvl2_2=".$user_id.",date2_2='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_3']=='0' && $np=='3'){
					$sql = "UPDATE $db->matrix_sixth SET lvl2_3=".$user_id.",date2_3='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}
				mysql_query($sql);
			}
			//second point
			$sql = "SELECT * FROM $db->matrix_sixth WHERE lvl1_2=".$node;
			$q = mysql_query($sql);							
			if(mysql_num_rows($q)>0){
				$sponsor_matrix=@mysql_fetch_assoc($q);
				if($sponsor_matrix['lvl2_4']=='0' && $np=='1'){
					$sql = "UPDATE $db->matrix_sixth SET lvl2_4=".$user_id.",date2_4='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_5']=='0' && $np=='2'){
					$sql = "UPDATE $db->matrix_sixth SET lvl2_5=".$user_id.",date2_5='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_6']=='0' && $np=='3'){
					$sql = "UPDATE $db->matrix_sixth SET lvl2_6=".$user_id.",date2_6='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}
				mysql_query($sql);
			}
			//second point
			$sql = "SELECT * FROM $db->matrix_sixth WHERE lvl1_3=".$node;
			$q = mysql_query($sql);
			if(mysql_num_rows($q)>0){
				$sponsor_matrix=@mysql_fetch_assoc($q);
				if($sponsor_matrix['lvl2_7']=='0' && $np=='1'){
					$sql = "UPDATE $db->matrix_sixth SET lvl2_7=".$user_id.",date2_7='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_8']=='0' && $np=='2'){
					$sql = "UPDATE $db->matrix_sixth SET lvl2_8=".$user_id.",date2_8='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}elseif($sponsor_matrix['lvl2_9']=='0' && $np=='3'){
					$sql = "UPDATE $db->matrix_sixth SET lvl2_9=".$user_id.",date2_9='".date('Y-m-d')."' WHERE id=".$sponsor_matrix['id'];
				}
				mysql_query($sql);
			}*/
		}
	}
}
function rebuild($rootid=1){	$t = @mysql_num_rows($r=mysql_query("select id,matrix_upline from gg_users where matrix_upline>0 or id=1 order by id"));			if($t<=0)	{		return false;	}	$arrTree = array();	while($user = mysql_fetch_assoc($r))	{		$nParentID = $user['matrix_upline'];		$dataTemp  = array();		$dataTemp['id']    = $user['id'];		$arrTree[$nParentID][] = $dataTemp;	}	$rootID = 0;	if((isset($arrTree[0])) && (sizeof($arrTree[0])>0) )	{		$rootID = $arrTree[0][0]['id'];	}	if($rootID<=0)	{		return false;	}	rebuild_tree($rootID,1,1,1, $arrTree);	return true;}function rebuild_tree($nParentID,$left,$nlevel,$seq, $arrTree){	$right = $left+1;	$subNodeInfo   = @(isset($arrTree[$nParentID]))?$arrTree[$nParentID]:array();	$subSize       = sizeof($subNodeInfo);	if($subSize>0)		$isparent = 1;	else		$isparent=0;	if($subSize>0)	{		$seqno = 0;		foreach($subNodeInfo as $key=>$nodeInfo)		{			$right = rebuild_tree($nodeInfo['id'],$right,$nlevel+1,$seqno+1, $arrTree);			$seqno++;		}	}	mysql_query("update gg_users set matrix_left = $left, matrix_right = $right, matrix_level = $nlevel where id = $nParentID");			return $right+1;}function rebuild2($rootid=1){	$t = @mysql_num_rows($r=mysql_query("select id,ref from gg_users where ref>0 or id=1 order by id"));			if($t<=0)	{		return false;	}	$arrTree2 = array();	while($user = mysql_fetch_assoc($r))	{		$nParentID = $user['ref'];		$dataTemp  = array();		$dataTemp['id']    = $user['id'];		$arrTree2[$nParentID][] = $dataTemp;	}	$rootID = 0;	if((isset($arrTree2[0])) && (sizeof($arrTree2[0])>0) )	{		$rootID = $arrTree2[0][0]['id'];	}	if($rootID<=0)	{		return false;	}	rebuild_tree2($rootID,1,1,1, $arrTree2);	return true;}function rebuild_tree2($nParentID,$left,$nlevel,$seq, $arrTree2){	$right = $left+1;	$subNodeInfo   = @(isset($arrTree2[$nParentID]))?$arrTree2[$nParentID]:array();	$subSize       = sizeof($subNodeInfo);	if($subSize>0)		$isparent = 1;	else		$isparent=0;	if($subSize>0)	{		$seqno = 0;		foreach($subNodeInfo as $key=>$nodeInfo)		{			$right = rebuild_tree2($nodeInfo['id'],$right,$nlevel+1,$seqno+1, $arrTree2);			$seqno++;		}	}	mysql_query("update gg_users set ref_left = $left, ref_right = $right, ref_level = $nlevel where id = $nParentID");			return $right+1;}
?>